/**
* 设置页脚的位置
*/
function set_footer_position() {
	var page_width = $(window).width();
	var footer_panel_left = (page_width - 128) / 2;
	$("#footer").css('left', footer_panel_left);
}


/**
 * 判断登录时的客户输入的内容是否正确
 */
function judge_login() {
	var account = $("#account").val();
	if (account == "") {
		$("#account_info").text("请输入帐户名");
		return;
	}

	var pattern = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; //判断邮箱格式的正则表达式
	var ismatch = pattern.test(account);
	if(!ismatch) {
		$("#account_info").text("请输入正确的邮箱格式");
		return;
	} else {
		$("#account_info").text("");
	}

	var password = $("#password").val();
	if(password == "") {
		$("#password_info a").text("请输入密码");
		return;
	} else if(password.length < 6) {
		$("#password_info a").text("密码必须不小于6位");
		return;
	} else {
		$("#password_info a").text("忘记密码？");
	}

	var request = createAjaxRequest();
	request.open("POST", "server/login.php", false);
	var username = $("#account").val();
	var password = $("#password").val();
	request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	request.send("username=" + username + "&password=" + password);
	var is_conflict = request.responseText;
	if(is_conflict == "0") {
		$("#account_info").text("邮箱不存在或密码错误");
		return;
	}
	window.location.href = "blog_home.html";
}

/**
* 设置登录页面的位置
*/
function login_table_position() {
	var page_width = $(window).width();
	var page_height = document.body.scrollHeight;
	var login_panel_left = (page_width - 572) / 2;
	$("#login_panel").css('left', login_panel_left);
	$("#login_background").css('width', page_width);
	$("#login_background").css('height', page_height);
}

/**
 * 绑定图片按钮的变化
 * @param id
 * @param normal_path
 * @param hover_path
 * @param active_path
 */
function img_button_bind(id, normal_path, hover_path, active_path) {
	$(id).hover(function() {
		$(this).attr("src", hover_path);
	}, function() {
		$(this).attr("src", normal_path);
	})
	
	$(id).mousedown(function() {
		$(this).attr("src", active_path);
	})
	
	$(id).mouseup(function() {
		$(this).attr("src", hover_path);
	})
}

/**
* 忘记密码的鼠标效果
*/
function forget_password() {
	$("#password_info a").hover(function() {
		$(this).css('color', 'blue');
	}, function() {
		$(this).css('color', '#0099FF');
	}).click(function() {
		window.open("find_password.html");
	});
}

/**
* 退出登录页面，返回到之前的页面
*/
function quit_login_panel() {
	$("#login_background").css('visibility', 'hidden');
	$("#login_panel").css('visibility', 'hidden');
}

/**
* 重定向到注册页面
*/
function redirect_to_register() {
	window.open("register.html");
}

/**
* 显示注册页面
*/
function show_login_panel() {
	$("#login_background").css('visibility', 'visible');
	$("#login_panel").css('visibility', 'visible');
}


/**
* header部分的按钮鼠标事件的绑定
*/
function header_button_action_bind() {
	button_hover_bind("#set_blog_button", "imgs/personal_home/set_blog_button_normal.png", "imgs/personal_home/set_blog_button_hover.png");
	button_hover_bind("#set_account_button", "imgs/personal_home/set_account_button_normal.png", "imgs/personal_home/set_account_button_hover.png");
	button_hover_bind("#exit_button", "imgs/personal_home/quit_button_normal.png", "imgs/personal_home/quit_button_hover.png");

	$("#set_blog_button").click(function() {
		window.location.href = "set_blog.html";
	});

	$("#set_account_button").click(function() {
		window.location.href = "set_personal.html";
	});

	$("#exit_button").click(function() {
		window.location.href = "index.html";
	});
}

/**
* 按钮的鼠标移入移除事件处理
*/
function button_hover_bind(id, normal, hover) {
	$(id).hover(function() {
		$(this).attr("src", hover);
	}, function() {
		$(this).attr("src", normal);
	});
}


/**
* publish部分的按钮鼠标事件的绑定
*/
function publish_button_action_bind() {
	button_hover_bind("#publish_text_button", "imgs/personal_home/text_normal.png", "imgs/personal_home/text_hover.png");
	button_hover_bind("#publish_photo_button", "imgs/personal_home/photo_normal.png", "imgs/personal_home/photo_hover.png");
	button_hover_bind("#publish_music_button", "imgs/personal_home/music_normal.png", "imgs/personal_home/music_hover.png");
	button_hover_bind("#publish_link_button", "imgs/personal_home/link_normal.png", "imgs/personal_home/link_hover.png");
	
	$("#publish_text_button").click(function() {
		window.open("publish_text.html");
	});

	$("#publish_photo_button").click(function() {
		window.open("publish_photo.html");
	});

	$("#publish_music_button").click(function() {
		window.open("publish_music.html");
	});

	$("#publish_link_button").click(function() {
		window.open("publish_link.html");
	});
}

/**
* 显示选择搜索类型的选择框
*/
function show_search_type_list() {
	$("#select").css('visibility', 'visible');
}


/**
* 隐藏选择搜索类型的选择框
*/
function hidden_search_type_list() {
	$("#select").css('visibility', 'hidden');
}

/**
* 选择搜索类型按钮的鼠标点击事件绑定
*/
function search_type_button_bind() {
	$("#search_blogs").click(function() {
		$("#search_text").val("搜索博客");
		$("#search_text").attr('name', 'blog');
		hidden_search_type_list();
	});
	$("#search_tags").click(function() {
		$("#search_text").val("搜索标签");
		$("#search_text").attr('name', 'tag');
		hidden_search_type_list();
	});
	$("#search_compositions").click(function() {
		$("#search_text").val("搜索我关注的文章");
		$("#search_text").attr('name', 'compositions');
		hidden_search_type_list();
	});

	$("#search_text").focus(function() {
		$(this).val("");
	}).blur(function() {
		var name = $(this).attr('name');
		if(name == 'blog') {
			$(this).val("搜索博客");
		} else if(name == 'tag') {
			$(this).val("搜索标签");
		} else {
			$(this).val("搜索我关注的文章");
		}
	})
}


/**
* 显示选择发布的博客面板
*/
function show_blog_list_panel() {
	$("#blog_list").css('visibility', 'visible');
}


/**
* 隐藏选择发布的博客面板
*/
function hidden_blog_list_panel() {
	$("#blog_list").css('visibility', 'hidden');
}


/**
* 标签部分的一些事件处理
*/
function tag_content_bind() {
	$(".blog_item").click(function() {
		var src = $(this).find("img").attr('src');
		var name = $(this).find("span").text();
		$("#current_blog_logo").attr('src', src);
		$("#current_blog_name").text(name);
		hidden_blog_list_panel();
	});

	$(".tag_input").focus(function() {
		$(this).val("");
	}).blur(function() {
		$(this).val("输入标签，用逗号或回车分隔");
	});

	$(".tag").hover(function() {
		$(this).css('background-color', '#3366FF');
	}, function() {
		$(this).css('background-color', '#3300CC');
	});
}

/**
* 回应按钮触发显示回应框事件绑定
*/
function reply_bind() {
	$(".reply").toggle(function() {
		$(this).parent().parent().parent().parent(".blog").append("<div class='reply_content'><div class='reply_text'><textarea></textarea></div><div class='reply_button'><p>回 应</p></div>");
	}, function() {
		$(this).parent().parent().parent().parent(".blog").find(".reply_content").remove();
	});
}